package com.ygxsk.carhome.views.picker;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;

import com.blankj.utilcode.util.StringUtils;
import com.ygxsk.carhome.R;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 月份选择器
 * Created by ycuwq on 17-12-28.
 */
public class MonthPicker extends WheelPicker<Integer> {

    private static final int MAX_MONTH = 12;
    private static final int MIN_MONTH = 1;

    private int mSelectedMonth;

    private OnMonthSelectedListener mOnMonthSelectedListener;

    private int mYear;
    private long mMaxDate, mMinDate;
    private int mMaxYear, mMinYear;
    private int mMinMonth = MIN_MONTH;
    private int mMaxMonth = MAX_MONTH;

    public MonthPicker(Context context) {
        this(context, null);
    }

    public MonthPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MonthPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //2020年01月03日是不是该在这里设置个性化内容
        initAttrs(context, attrs);

        setItemMaximumWidthText("000");

        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);

        Calendar.getInstance().clear();

        mSelectedMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;

        updateMonth();

        setSelectedMonth(mSelectedMonth, false);

        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {

            @Override
            public void onWheelSelected(Integer item, int position, String itemString) {

                mSelectedMonth = item;

//                LogUtils.d("itchen--MonthPicker--月份滚轮---选中的【月】-->" + item);

                if (mOnMonthSelectedListener != null) {
                    mOnMonthSelectedListener.onMonthSelected(item, itemString);
                }
            }

        });
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        mSelectedMonth = Calendar.getInstance().get(Calendar.MONTH);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.YearMonthDayPicker);
        int textSize = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemTextSize,
                getResources().getDimensionPixelSize(R.dimen.WheelItemTextSize));
        int textColor = a.getColor(R.styleable.YearMonthDayPicker_itemTextColor,
                Color.BLACK);
        boolean isTextGradual = a.getBoolean(R.styleable.YearMonthDayPicker_textGradual, true);
        boolean isCyclic = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCyclic, false);
        int halfVisibleItemCount = a.getInteger(R.styleable.YearMonthDayPicker_halfVisibleItemCount, 2);
        int selectedItemTextColor = a.getColor(R.styleable.YearMonthDayPicker_selectedTextColor,
                getResources().getColor(R.color.com_ycuwq_datepicker_selectedTextColor));
        int selectedItemTextSize = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_selectedTextSize,
                getResources().getDimensionPixelSize(R.dimen.WheelSelectedItemTextSize));
        int itemWidthSpace = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemWidthSpace,
                getResources().getDimensionPixelOffset(R.dimen.WheelItemWidthSpace));
        int itemHeightSpace = a.getDimensionPixelSize(R.styleable.YearMonthDayPicker_itemHeightSpace,
                getResources().getDimensionPixelOffset(R.dimen.WheelItemHeightSpace));
        boolean isZoomInSelectedItem = a.getBoolean(R.styleable.YearMonthDayPicker_zoomInSelectedItem, true);
        boolean isShowCurtain = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCurtain, true);
        int curtainColor = a.getColor(R.styleable.YearMonthDayPicker_wheelCurtainColor, Color.WHITE);
        boolean isShowCurtainBorder = a.getBoolean(R.styleable.YearMonthDayPicker_wheelCurtainBorder, true);
        int curtainBorderColor = a.getColor(R.styleable.YearMonthDayPicker_wheelCurtainBorderColor,
                getResources().getColor(R.color.com_ycuwq_datepicker_divider));
        a.recycle();

        setTextSize(textSize);

        setTextColor(textColor);

        setTextGradual(isTextGradual);

        isCyclic = true;//默认设置为是无限循环的
        setCyclic(isCyclic);

        setHalfVisibleItemCount(halfVisibleItemCount);

        setSelectedItemTextColor(selectedItemTextColor);

        setSelectedItemTextSize(selectedItemTextSize);


        setItemWidthSpace(itemWidthSpace);

        setItemHeightSpace(itemHeightSpace);

        setZoomInSelectedItem(isZoomInSelectedItem);

        setShowCurtain(isShowCurtain);

        setCurtainColor(curtainColor);

        setShowCurtainBorder(isShowCurtainBorder);

        setCurtainBorderColor(curtainBorderColor);

    }

    public void updateMonth() {

        List<Integer> list = new ArrayList<>();
        List<String> listString = new ArrayList<>();

        for (int i = mMinMonth; i <= mMaxMonth; i++) {
            list.add(i);
            //listString.add(String.format("%02d", i) + "月");
            listString.add(String.format("%02d", i) + StringUtils.getString(R.string.unit_month));
        }

        //最后添加一个：清空重置
//        list.add(13);
//        listString.add(StringUtils.getString(R.string.chooseseingnull));

        setDataList(list, listString);

    }

    public void setMaxDate(long date) {
        mMaxDate = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        mMaxYear = calendar.get(Calendar.YEAR);
    }

    public void setMinDate(long date) {
        mMinDate = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        mMinYear = calendar.get(Calendar.YEAR);
    }


    public void setYear(int year) {
        mYear = year;
        mMinMonth = MIN_MONTH;
        mMaxMonth = MAX_MONTH;
        if (mMaxDate != 0 && mMaxYear == year) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mMaxDate);
            mMaxMonth = calendar.get(Calendar.MONTH) + 1;

        }
        if (mMinDate != 0 && mMinYear == year) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mMinDate);
            mMinMonth = calendar.get(Calendar.MONTH) + 1;

        }
        updateMonth();
        if (mSelectedMonth > mMaxMonth) {
            setSelectedMonth(mMaxMonth, false);
        } else if (mSelectedMonth < mMinMonth) {
            setSelectedMonth(mMinMonth, false);
        } else {
            setSelectedMonth(mSelectedMonth, false);
        }
    }

    public int getSelectedMonth() {
        return mSelectedMonth;
    }

    public void setSelectedMonth(int selectedMonth) {
        setSelectedMonth(selectedMonth, true);
    }

    public void setSelectedMonth(int selectedMonth, boolean smoothScroll) {
        setCurrentPosition(selectedMonth - mMinMonth, smoothScroll);
        mSelectedMonth = selectedMonth;
    }

    public void setOnMonthSelectedListener(OnMonthSelectedListener onMonthSelectedListener) {
        mOnMonthSelectedListener = onMonthSelectedListener;
    }

    public interface OnMonthSelectedListener {
        void onMonthSelected(int month, String monthString);
    }

}
